package com.hsg.lsn13.account;

import android.accounts.Account;
import android.accounts.AccountManager;
import android.content.ContentResolver;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;

public class AccountHelper {

    public static final String ACTION_TYPE = "com.hsg.lsn13.account";

    public static void addAccount(Context context) {
        AccountManager am = (AccountManager) context.getSystemService(Context.ACCOUNT_SERVICE);
        Account[] accountsByType = am.getAccountsByType(ACTION_TYPE);//写时会自动要加个权限配置
        if (accountsByType.length > 0) {
            Log.e("lsn13", "账户已存在");
            return;
        }
        Account account = new Account("hsg", ACTION_TYPE);
        am.addAccountExplicitly(account, "hsg", new Bundle());//写时会自动要加个权限配置

    }

    public static void autoSync() {
        String contentAuthority = "com.hsg.lsn13.provider";
        Account account = new Account("hsg", ACTION_TYPE);
        ContentResolver.setIsSyncable(account, contentAuthority, 1);//写时会自动要加个WRITE_SYNC_SETTINGS权限
        ContentResolver.setSyncAutomatically(account, contentAuthority, true);
        ContentResolver.addPeriodicSync(account, contentAuthority, new Bundle(), 1);
    }


}
